<?php
include "../../../lib/list.php";
if ($_GET['s_no']){
	$s_no = $_GET['s_no'];
	$sear_char .= " AND uatp_stock.s_no = $s_no";
}

if ($_POST['s_no']){
	$s_no = $_POST['s_no'];
	$sear_char .= " AND uatp_stock.s_no = $s_no";
}
if ($_GET['sg_no'])$sg_no = $_GET['sg_no'];
if ($_POST['sg_no'])$sg_no = $_POST['sg_no'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us">
<head>
<link rel="stylesheet" href="../../../css/list.css" type="text/css"></link>
<link rel="stylesheet" href="../../../css/insert.css" type="text/css"></link>
<link rel="stylesheet" href="../../../css/table.sorter.css" type="text/css" media="print, projection, screen" />
<link type="text/css" rel="stylesheet" href="../../../css/dhtmlgoodies_calendar.css?random=20051112" media="screen"></link>

<script type="text/javascript" src="../../../js/dhtmlgoodies_calendar.js?random=20060118"></script>
<script type="text/javascript" src="../../../js/list.js"></script>
<script type="text/javascript" src="../../../js/jquery-1.2.6.js"></script>
<script type="text/javascript" src="../../../js/jquery.tablesorter.js"></script>
<script type="text/javascript">
function page_move(page){
			document.view.page.value = page;
			document.view.target = 'right';
			document.view.action = 'view.php';
			document.view.method = 'post';
			document.view.submit();
}

$(function(){
	$("select[name='sg_no']").change(function(){
	    var sg_no = $("select[name='sg_no']").attr('value');
	    $.get('../../../lib/select.php?sg_no='+sg_no,function(res){
	        $("select[name='s_no']").html(res);
	    });
	});
});


function movepage(url,get,value){
	var sg_no = document.getElementsByName('sg_no')[0].value;
	location.href = url+"?"+get+"="+value+"&sg_no="+sg_no;
}


	</script>
</head>
<body>
	<div style="width: 1024px;">
		<div
			style="border-bottom: 1px solid #e8e8e8; text-align: left; font-size: 12px; margin-bottom: 12px; color: #9a9a9a">:::
			재고 관리&nbsp;⇒&nbsp;재고파악전표</div>
			<div style="float: left; padding: 5px; font-size:11px;">
						<select name='sg_no' class="default">
						<option value="-3">:: 점포그룹 선택 ::</option>
						<?
						$qry = "SELECT * FROM uatp_shopgroup order by no DESC";
						$query = DBquery($qry);
						while($list = mysql_fetch_array($query)){
							?>
						<option value="<?=$list['no']?>"<?if($list['no']==$sg_no)echo " selected";?>><?=$list['name']?></option>
						<?
						}
						mysql_close();
						?>
						</select>
						
						<select name='s_no' class="default" onChange="movepage('allstockview.php','s_no',this.value);";>
							<option value="">:: 점포 선택 ::</option>
							<?
							if($sg_no){
								$qry = "SELECT * FROM uatp_shopinfo WHERE sg_no = $sg_no order by no DESC";
								$query = DBquery($qry);
								while($list = mysql_fetch_array($query)){
									?>
														<option value="<?=$list['no']?>"<?if($list['no']==$s_no)echo " selected";?>><?=$list['name']?></option>
														<?
														}
														mysql_close();
							}
							?>
						</select>
			</div>
			<form name="detail" method="post" action="query.php" enctype=multipart/form-data>
						<input type="hidden" name="page" value=""/>
						<input type="hidden" value="<?=$s_no?>" name="s_no" />
						<input type="hidden" value="<?=$sg_no?>" name="sg_no" />
			<table cellspacing="1" class="tablesorter">
			<thead>
				<tr>
								<th>점포명</th>
								<th>barcode</th>
								<th>상품명</th>
								<th>누계입고수</th>
								<th>누계결함품수</th>
								<th>누계판매수</th>
								<th>누계이동수</th>
								<th>이론재고수</th>
								<th>이론재고금액</th>
								<th>재고파악재고수</th>
								<th>재고파악재고금액</th>
								<th>차이수</th>
								<th>차이금액</th>
								</tr>
								<tr>
				</tr>
			</thead>
			<?php 
			$page=$_POST['page'];
			
			$sql = "SELECT uatp_product.price1, uatp_product.no AS 'p_no', uatp_stock.s_no
					, uatp_product.productname, uatp_product.barcode
					, (IFNULL(review.alde,0)+IFNULL(defect.de_count,0)) AS defect, (IFNULL(move.mo_count,0)+IFNULL(review.almo,0)) AS move
					, (IFNULL(ibgo.ib_count,0)+IFNULL(review.allin,0)) AS ibgo
					, IFNULL(review.algap,0) AS gap, review.date, review.lastdate
					, ((IFNULL(review.alde,0)+IFNULL(defect.de_count,0))+(IFNULL(move.mo_count,0)+IFNULL(review.almo,0))+(IFNULL(ibgo.ib_count,0)+IFNULL(review.allin,0))) AS theory
					, uatp_stock.count AS realstock, uatp_shopinfo.name AS shopname
						
					FROM uatp_stock
					INNER JOIN uatp_product ON uatp_stock.p_no = uatp_product.no
					INNER JOIN uatp_shopinfo ON uatp_stock.s_no = uatp_shopinfo.no
					
					LEFT OUTER JOIN
					(SELECT uatp_stockreview.`s_no`, uatp_stockreview.`p_no`,(`total`.allin+`total`.alde+`total`.almo+`total`.alsa) AS theory , uatp_stockreview.`realstock`,
						uatp_stockreview.`date`, uatp_stockreview.`lastdate`, `total`.allin, `total`.alde, `total`.almo, `total`.alsa, `total`.algap
					
					FROM `uatp_stockreview` 
					
					LEFT OUTER JOIN 
					(SELECT `s_no`, `p_no`, sum(`instock`) AS `allin`, sum(`defect`) AS `alde`, sum(`move`) AS `almo`, sum(`sale`) AS
					`alsa`, sum(`gap`) AS `algap` FROM `uatp_stockreview` 
					group by s_no, p_no) AS total ON uatp_stockreview.s_no = total.s_no AND uatp_stockreview.p_no = total.p_no
					
					WHERE `date` IN (SELECT MAX(date) FROM `uatp_stockreview` GROUP BY `s_no`, `p_no`) GROUP BY `s_no`, `p_no`
					) AS review ON uatp_stock.p_no = review.p_no AND uatp_stock.s_no = review.s_no
					
					LEFT OUTER JOIN 
					(SELECT p_no, s_no, sum(count) AS de_count, sum(price) AS de_price
					FROM uatp_prdreview
					WHERE unit =1
					GROUP BY p_no,s_no
					) AS defect ON uatp_stock.p_no = defect.p_no AND uatp_stock.s_no = defect.s_no
								
					LEFT OUTER JOIN 
					(SELECT p_no, s_no, sum(count) AS mo_count, sum(price) AS mo_price
					FROM uatp_prdreview
					WHERE unit =2
					GROUP BY p_no,s_no
					) AS move ON uatp_stock.p_no = move.p_no AND uatp_stock.s_no = move.s_no
										
					LEFT OUTER JOIN 
					(SELECT p_no, s_no, sum(count) AS ib_count, sum(price) AS ib_price
					FROM uatp_prdreview
					WHERE unit = 0
					GROUP BY p_no,s_no
					) AS ibgo ON uatp_stock.p_no = ibgo.p_no AND uatp_stock.s_no = ibgo.s_no		
					
					WHERE 1 $sear_char GROUP BY uatp_stock.p_no, uatp_stock.s_no ORDER BY uatp_stock.s_no";
			
			$total = DBnumrows($sql);
			
			$scale=15;
			if ($page == ''){
				$page=1;
			}
			
			$cpage = intval($page);//현재 페이지
			$totalpage = intval($total/$scale); //전체 페이지 갯수
			if ($totalpage*$scale != $total)
			$totalpage = $totalpage + 1;
			
			if ($cpage ==1) {
				$cline = 0 ;
			} else {
				$cline = ($cpage*$scale) - $scale ;
			}
			
			$limit=$cline+$scale;
			
			if ($limit >= $total)
			$limit=$total;
			
			$scale1 = $limit - $cline;
			
			$query = $sql." DESC LIMIT $cline, $scale1";
			$total = DBnumrows($query);
			$result = DBquery($query);
			
			for($i=1; $list = mysql_fetch_array($result); $i++){
												
				?>
			<tbody>
			<tr>
				<td><?=$list['shopname']?></td>
				<td><?=$list['barcode']?></td>
				<td><?=$list['productname']?></td>
				<td><?=$list['ibgo']?></td>
				<td><?=$list['defect']?></td>
				<td>누계판매수</td>
				<td><?=$list['move']?></td>
				<td><?=$list['theory']?></td> 										<!-- 입고수 + 결함품수 + 상품이동수 		= 이론재고	-->
				<td><?=$list['theory']*$list['price1']?></td> 						<!-- 입고가격 + 결함품가격 + 상품이동가격	= 이론금액	-->
				<td><?=$list['realstock']?></td>									<!-- 현재 재고수										-->
				<td><?=$list['now_count']*$list['price1']?></td> 					<!-- 현재 금액										-->
				<td><?=$list['gap']?></td>											<!-- 차이수	-->
				<td><?=$list['gap']*$list['price1']?></td>							<!--차이금액	-->
			</tr>
			</tbody>
			<?php 
				}
				mysql_free_result($result);
			?>
		</table>
		<div style="text-align:left;float:left;padding:0;margin:0;width:700px;">
		<?php page_avg($totalpage,$cpage);?>	
						</div>
			</form>
	</div>
</body>
</html>
